#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "led.h"
#include "key.h"
#include "OLED.h"
#include "CountSensor.h"
#include "Timer.h"
#include "PWM.h"
#include "Servo.h"
int main()
{
	OLED_Init();
	float angle = 0;
	Servo_Init(RCC_APB2Periph_GPIOA, GPIOA, GPIO_Pin_1, GPIO_Mode_AF_PP, RCC_APB1Periph_TIM2, TIM2, TIM_CKD_DIV1, TIM_CounterMode_Up, 20000 - 1, 
	72 - 1, 0, TIM_OCMode_PWM1, TIM_OCPolarity_High, 0, 1);//SG90舵机要求输入PWM频率为50Hz，高电平时间为0.5ms到2.5ms，亦即占空比为500到2500
	gpio_Init(RCC_APB2Periph_GPIOA, GPIOA, GPIO_Pin_2, GPIO_Mode_IPU);//Pin_2下拉输入接开关，按一下角度自增
	gpio_Init(RCC_APB2Periph_GPIOA, GPIOA, GPIO_Pin_4, GPIO_Mode_IPU);//Pin_4下拉输入接开关，按一下角度自减
	while(1)
	{
		if(key_read_value_digital(GPIOA, GPIO_Pin_2))
		{
			angle += 10.0;
		}
		else if(key_read_value_digital(GPIOA, GPIO_Pin_4))
		{
			angle -= 10.0;
		}
		if((int)angle == 190 || (int)angle == -10)
		{
			angle = 0.0;
		}
		Servo_SetAngle(TIM2, angle, 1);
		OLED_ShowNum(1, 1, angle, 3);
	}
}
